package com.zzsy.shoppingpro.commentactivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.base.BaseActivity;
import com.zzsy.shoppingpro.home.bean.GoodsBean;
import com.zzsy.shoppingpro.home.utils.VirtualkeyboardHeight;
import com.zzsy.shoppingpro.shoppingcart.adapter.NumberAddSubView;
import com.zzsy.shoppingpro.shoppingcart.utils.CartProvider;
import com.zzsy.shoppingpro.utils.StringUtils;
import com.zzsy.shoppingpro.utils.Toast;

import java.io.Serializable;
import java.util.List;

public class GoodsInfoActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton ibGoodInfoBack;
    private ImageButton ibGoodInfoMore;
    private ImageView ivGoodInfoImage;
    private TextView tvGoodInfoName;
    private TextView tvGoodInfoDesc;
    private TextView tvGoodInfoPrice;
    private TextView tvGoodInfoStore;
    private TextView tvGoodInfoStyle;
    private WebView wbGoodInfoMore;
    private TextView tvGoodInfoCallcenter;
    private TextView tvGoodInfoCollection;
    private TextView tvGoodInfoCart;
    private Button btnGoodInfoAddcart;
    private TextView tvMoreShare;
    private TextView tvMoreSearch;
    private TextView tvMoreHome;
    private LinearLayout ll_root;
    private Button btn_more;

    private CartProvider cartProvider;

    private List<GoodsBean> goodsBeans;
    private GoodsBean goodsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        findViews();

        cartProvider = CartProvider.getInstance();
        //取出Intent
        Intent intent = getIntent();
        goodsBean = (GoodsBean) intent.getSerializableExtra("goods_bean");

        if(goodsBean != null){
            //本地存储的数据
            setDataFormView(goodsBean);
        }

    }

    public void setDataFormView(GoodsBean goodsBean) {
        String name = goodsBean.getName();
        String cover_price = goodsBean.getCover_price();
        String figure = goodsBean.getFigure();
        String product_id = goodsBean.getProduct_id();
        Glide.with(this).load(Config.BASE_URl_IMAGE + figure).into(ivGoodInfoImage);
        if(!StringUtils.isEmpty(name)){
            tvGoodInfoName.setText(name);
        }
        if(!StringUtils.isEmpty(cover_price)){
            tvGoodInfoPrice.setText("￥" + cover_price);
        }
        setWebview(product_id);
    }

    private void setWebview(String product_id) {
       if(product_id != null){
           wbGoodInfoMore.loadUrl("http://www.atguigu.com");
           //覆盖webview默认使用第三方或系统默认浏览器打开网页的行为，试网页用webview打开
           wbGoodInfoMore.setWebViewClient(new WebViewClient(){
               @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                   return true;
               }
           });
           //启用支持javaSript
           WebSettings settings = wbGoodInfoMore.getSettings();
           settings.setJavaScriptEnabled(true);
           settings.setUseWideViewPort(true);

           //优先使用缓存
           wbGoodInfoMore.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
       }
    }

    /**
     * 初始化数据
     * */
    private void findViews() {
        ibGoodInfoBack =  findViewById(R.id.ib_good_info_back);
        ibGoodInfoMore =  findViewById(R.id.ib_good_info_more);
        ivGoodInfoImage =  findViewById(R.id.iv_good_info_image);
        tvGoodInfoName =  findViewById(R.id.tv_good_info_name);
        tvGoodInfoDesc =  findViewById(R.id.tv_good_info_desc);
        tvGoodInfoPrice =  findViewById(R.id.tv_good_info_price);
        tvGoodInfoStore =  findViewById(R.id.tv_good_info_store);
        tvGoodInfoStyle =  findViewById(R.id.tv_good_info_style);
        wbGoodInfoMore =  findViewById(R.id.wb_good_info_more);

        tvGoodInfoCallcenter =  findViewById(R.id.tv_good_info_callcenter);
        tvGoodInfoCollection =  findViewById(R.id.tv_good_info_collection);
        tvGoodInfoCart =  findViewById(R.id.tv_good_info_cart);
        btnGoodInfoAddcart =  findViewById(R.id.btn_good_info_addcart);

        ll_root =  findViewById(R.id.ll_root);
        tvMoreShare =  findViewById(R.id.tv_more_share);
        tvMoreSearch =  findViewById(R.id.tv_more_search);
        tvMoreHome =  findViewById(R.id.tv_more_home);

        btn_more =  findViewById(R.id.btn_more);

        ibGoodInfoBack.setOnClickListener(this);
        ibGoodInfoMore.setOnClickListener(this);
        btnGoodInfoAddcart.setOnClickListener(this);

        tvMoreShare.setOnClickListener(this);
        tvMoreSearch.setOnClickListener(this);
        tvMoreHome.setOnClickListener(this);

        btn_more.setOnClickListener(this);

        tvGoodInfoCallcenter.setOnClickListener(this);
        tvGoodInfoCollection.setOnClickListener(this);
        tvGoodInfoCart.setOnClickListener(this);
        btnGoodInfoAddcart.setOnClickListener(this);
        tvGoodInfoCallcenter.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        if (v == ibGoodInfoBack) {
            finish();
        } else if (v == ibGoodInfoMore) {
            if (ll_root.getVisibility() == View.VISIBLE) {
                ll_root.setVisibility(View.GONE);
            } else {
                ll_root.setVisibility(View.VISIBLE);
            }
        } else if (v == btn_more) {
            ll_root.setVisibility(View.GONE);
        } else if (v == tvMoreShare) {
            android.widget.Toast.makeText(GoodsInfoActivity.this, "分享", android.widget.Toast.LENGTH_SHORT).show();
        } else if (v == tvMoreSearch) {
            android.widget.Toast.makeText(GoodsInfoActivity.this, "搜索", android.widget.Toast.LENGTH_SHORT).show();
        } else if (v == tvMoreHome) {
            finish();
        } else if (v == tvGoodInfoCallcenter) {
            android.widget.Toast.makeText(GoodsInfoActivity.this, "客服", android.widget.Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, CallCenterActivity.class);
            startActivity(intent);
        } else if (v == tvGoodInfoCollection) {
            android.widget.Toast.makeText(GoodsInfoActivity.this, "收藏", android.widget.Toast.LENGTH_SHORT).show();
        } else if (v == tvGoodInfoCart) {
            Intent intent = new Intent(this, ShoppingCartActivity.class);
            startActivity(intent);

        } else if (v == btnGoodInfoAddcart) {
            //添加购物车
            showPopwindow();
        }
    }

    /**
     * 显示popupWindow
     * */
    private void showPopwindow(){

        //利用layoutInflater 获得view
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popupwindow_add_product,null);

        //下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth();
        final PopupWindow window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT
                );

        //参数设置
        //设置popwindow弹出窗体可点击，这句话必须添加，并且是true
        window.setFocusable(true);

        //实例化一个colorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        window.setBackgroundDrawable(dw);

        //设置popWindow的现实和消失的动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);

        // 4 控件处理
        ImageView iv_goodinfo_photo = view.findViewById(R.id.iv_goodinfo_photo);
        TextView tv_goodinfo_name =  view.findViewById(R.id.tv_goodinfo_name);
        TextView tv_goodinfo_price = view.findViewById(R.id.tv_goodinfo_price);
        NumberAddSubView nas_goodinfo_num = view.findViewById(R.id.nas_goodinfo_num);
        Button bt_goodinfo_cancel = view.findViewById(R.id.bt_goodinfo_cancel);
        Button bt_goodinfo_confim =  view.findViewById(R.id.bt_goodinfo_confim);

        //加载图片
        Glide.with(GoodsInfoActivity.this).load(Config.BASE_URl_IMAGE + goodsBean.getFigure()).into(iv_goodinfo_photo);
        //名称
        tv_goodinfo_name .setText(goodsBean.getName());
        //显示价格
        tv_goodinfo_price.setText(goodsBean.getCover_price());

        //设置最大值和当前值
        nas_goodinfo_num.setMaxValue(8);
        nas_goodinfo_num.setValue(1);

        nas_goodinfo_num.setOnNumberChangeListener(new NumberAddSubView.OnNumberChangeListener() {
            @Override
            public void addNumber(View view, int value) {
                goodsBean.setNumber(value);
            }

            @Override
            public void subNumber(View view, int value) {
                goodsBean.setNumber(value);
            }
        });

        bt_goodinfo_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });

        bt_goodinfo_confim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
                //添加购物车
                cartProvider.addData(goodsBean);
                Toast.show(GoodsInfoActivity.this,"添加购物车成功！");
            }
        });

        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                window.dismiss();
            }
        });

        // 5 在底部显示
        window.showAtLocation(GoodsInfoActivity.this.findViewById(R.id.ll_goods_root),
                Gravity.BOTTOM, 0, VirtualkeyboardHeight.getBottomStatusHeight(GoodsInfoActivity.this));
    }

}
